﻿using Manage.Model;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Manage.Basic
{
    public partial class FrmUserMgr : Form
    {
        public FrmUserMgr()
        {
            InitializeComponent();
        }
        private List<AreaProvinceModel> prList;
        private List<AreaCityModel> ctList;
        private List<AreaDistrictModel> dtList;
        private List<SBProvinceModel> sbprList;
        private List<SBCityModel> sbctList;
        private List<SBDistrictModel> sbdtList;
        private List<TradeBigModel> tbList;
        private List<TradeMidModel> tmLsit;
        private List<TradeSmallModel> tsList;
        private bool isInit;
        private bool isSBInit;
        private bool isTradeInit;
        private int Sid;
        private string SLoginName;
        private string SCompName;
        private string SContact;
        private string SMobPhone;
        private string STaxNumber;
        private string SAddress;
        private string SBankName;
        private string SBankNum;
        private string STelNum;
        private int SProvinceId;
        private string SProvinceName;
        private int SCityId;
        private string SCityName;
        private int SDistrictId;
        private string SDistrictName;
        private string SSBProvinceId;
        private string SSBProvinceName;
        private string SSBCityId;
        private string SSBCityName;
        private string SSBDistrictId;
        private string STradeBigId;
        private string STradeBigName;
        private string STradeMidId;
        private string STradeMidName;
        private string STradeSmallId;
        private string STradeSmallName;
        private int SDeviceID;
        private string SDeviceRemark;
        private int SState;
        
        private DateTime SExpireDate;

        /// <summary>  
        /// 载入窗口   
        /// </summary> 
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FrmUserMgr_Load(object sender, EventArgs e)
        {
            
        }

        private void FrmUserMgr_Shown(object sender, EventArgs e)
        {
            try
            {
                isInit = true;
                //读取地区列表
                Dictionary<string, string> dcProvince = new Dictionary<string, string>();
                dcProvince.Add("Token", Share.MoM.Token);
                RetModel rm = Share.PostData("51API/Company/GetProvinceList", dcProvince, null);
                if (!rm.Success)
                {
                    MessageBox.Show("读取省份列表失败！\r\n错误代码:" + rm.ErrCode + "\r\n错误信息:" + rm.ErrInfo);
                }
                else
                {
                    AreaProvinceModel[] pr = JsonConvert.DeserializeObject<AreaProvinceModel[]>(rm.Data.ToString());
                    prList = pr.ToList();
                }
                //读取税务机关列表
                Dictionary<string, string> dcSBProvince = new Dictionary<string, string>();
                dcSBProvince.Add("Token", Share.MoM.Token);
                rm = Share.PostData("51API/Company/GetSBProvinceList", dcSBProvince, null);
                if (!rm.Success)
                {
                    MessageBox.Show("读取税务机关列表失败！\r\n错误代码:" + rm.ErrCode + "\r\n错误信息:" + rm.ErrInfo);
                }
                else
                {
                    SBProvinceModel[] sbpr = JsonConvert.DeserializeObject<SBProvinceModel[]>(rm.Data.ToString());
                    sbprList = sbpr.ToList();
                }
                //读取行业类别列表
                Dictionary<string, string> dcTrade = new Dictionary<string, string>();
                dcTrade.Add("Token", Share.MoM.Token);
                rm = Share.PostData("51API/Company/GetTradeBigList", dcTrade, null);
                if (!rm.Success)
                {
                    MessageBox.Show("读取行业分类列表失败！\r\n错误代码:" + rm.ErrCode + "\r\n错误信息:" + rm.ErrInfo);
                }
                else
                {
                    TradeBigModel[] tbm = JsonConvert.DeserializeObject<TradeBigModel[]>(rm.Data.ToString());
                    tbList = tbm.ToList();
                }

                Dictionary<string, string> dcDevice = new Dictionary<string, string>();
                dcDevice.Add("Token", Share.MoM.Token);
                rm = Share.PostData("", dcDevice, null);
                if (!rm.Success)
                {
                    MessageBox.Show("读取设备列表失败！\r\n错误代码:" + rm.ErrCode + "\r\n错误信息:" + rm.ErrInfo);
                }
                else
                {
                    DeviceInfoModel[] dim = JsonConvert.DeserializeObject<DeviceInfoModel[]>(rm.Data.ToString());
                    DataTable dt = new DataTable();
                    dt.Columns.Add("id");
                    dt.Columns.Add("设备备注");
                    for (int i = 0; i < dim.Length; i++)
                    {
                        DataRow dr = dt.NewRow();
                        dr["id"] = dim[i].id;
                        dr["设备备注"] = dim[i].Remark;
                        dt.Rows.Add(dr);
                    }
                    cbxDevice.DataSource = dt;
                    cbxDevice.ValueMember = "id";
                    cbxDevice.DisplayMember = "设备备注";
                }
                //绑定数据源
                cbxProvince.DisplayMember = "Name";
                cbxProvince.ValueMember = "Id,CityList";
                cbxProvince.DataSource = prList;
                cbxProvince.SelectedIndex = -1;

                cbxSBProvince.DisplayMember = "Name";
                cbxSBProvince.ValueMember = "id,CityList";
                cbxSBProvince.DataSource = sbprList;
                cbxSBProvince.SelectedIndex = -1;

                cbxTradeBig.DisplayMember = "Name";
                cbxTradeBig.ValueMember = "Id,SubTrades";
                cbxTradeBig.DataSource = tbList;
                cbxTradeBig.SelectedIndex = -1;
                isInit = false;
            }
            catch (Exception ex)
            {
                isInit = false;
                MessageBox.Show(ex.Message);
                Dispose();
            }
        }
        private void cbxProvince_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (isInit)
                return;
            try
            {
                isInit = true;
                var obj = cbxProvince.SelectedValue;
                if (null == obj)
                {
                    cbxCity.DataSource = null;
                    isInit = false;
                    return;
                }
                cbxCity.DataSource = null;
                var cl = (AreaProvinceModel)cbxProvince.SelectedValue;
                ctList = cl.Cities.ToList();
                cbxCity.DisplayMember = "Name";
                cbxCity.ValueMember = "id,DistrictList";
                cbxCity.DataSource = ctList;
                cbxCity.SelectedIndex = -1;
                isInit = false;
            }
            catch (Exception)
            {
                isInit = false;
                throw;
            }
        }
        private void cbxCity_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (isInit)
                return;
            try
            {
                isInit = true;
                var obj = cbxCity.SelectedValue;
                if (null == obj)
                {
                    cbxDistrict.DataSource = null;
                    isInit = false;
                    return;
                }
                var ct = (AreaCityModel)cbxCity.SelectedValue;
                dtList = ct.Districts.ToList();
                cbxDistrict.DisplayMember = "Name";
                cbxDistrict.ValueMember = "id";
                cbxDistrict.DataSource = dtList;
            }
            catch (Exception)
            {
                isInit = false;
                throw;
            }
        }
        private void cbxSBProvince_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (isSBInit)
                return;
            try
            {
                var obj = cbxSBProvince.SelectedValue;
                if (null == obj)
                {
                    cbxSBCity.DataSource = null;
                    isSBInit = false;
                    return;
                }
                sbctList = ((SBProvinceModel)obj).Cities.ToList();
                cbxSBCity.DisplayMember = "Name";
                cbxSBCity.ValueMember = "id,DistrictList";
                cbxSBCity.DataSource = sbctList;
                cbxSBCity.SelectedIndex = -1;
                cbxSBDistrict.DataSource = null;
                isSBInit = false;
            }
            catch (Exception)
            {
                isSBInit = false;
                throw;
            }
        }
        private void cbxSBCity_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (isSBInit)
                return;
            try
            {
                isSBInit = true;
                var obj = cbxSBCity.SelectedValue;
                if (null == obj)
                {
                    cbxSBDistrict.DataSource = null;
                    isSBInit = false;
                    return;
                }
                sbdtList = ((SBCityModel)obj).Districts.ToList();
                cbxSBDistrict.DisplayMember = "Name";
                cbxSBDistrict.ValueMember = "id";
                cbxSBDistrict.DataSource = sbdtList;
                cbxSBDistrict.SelectedIndex = -1;
                isSBInit = false;
            }
            catch (Exception)
            {

                throw;
            }
        }
        private void cbxTradeBig_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (isTradeInit)
                return;
            try
            {
                isTradeInit = true;
                var obj = cbxTradeBig.SelectedValue;
                if (null == obj)
                {
                    cbxTradeMid.DataSource = null;
                    isTradeInit = false;
                    return;
                }
                tmLsit = ((TradeBigModel)obj).SubTrades.ToList();
                cbxTradeMid.DisplayMember = "Name";
                cbxTradeMid.ValueMember = "id";
                cbxTradeMid.DataSource = tmLsit;
                cbxTradeMid.SelectedIndex = -1;
                isTradeInit = false;
            }
            catch (Exception)
            {

                throw;
            }
        }
        private void cbxTradeMid_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (isTradeInit)
                return;
            try
            {
                isTradeInit = true;
                var obj = cbxTradeMid.SelectedValue;
                if (null == obj)
                {
                    cbxTradeSmall.DataSource = null;
                    isTradeInit = false;
                    return;
                }
                tsList = ((TradeMidModel)obj).SubTrades.ToList();
                cbxTradeSmall.DisplayMember = "Name";
                cbxTradeSmall.ValueMember = "id";
                cbxTradeSmall.DataSource = tsList;
                cbxTradeSmall.SelectedIndex = -1;
                isTradeInit = false;
            }
            catch (Exception)
            {

                throw;
            }

        }
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void barButtonItem1_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            try
            {
                if (string.IsNullOrEmpty(tbxUserName.Text))
                    throw new Exception("用户名不能为空！");
                if (string.IsNullOrEmpty(tbxPassword.Text))
                    throw new Exception("密码不能为空！");
                if (tbxCompanyName.EditValue == null)
                    throw new Exception("单位名必须填写！");
                if (tbxContact.EditValue == null)
                    throw new Exception("联系人必须填写！");
                if (tbxMobPhone.EditValue == null)
                    throw new Exception("手机号必须填写！");
                if (tbxTaxNumber.EditValue == null)
                    throw new Exception("税号必须填写！");
                if (tbxAddress.EditValue == null)
                    throw new Exception("联系地址必须填写！");
                if (tbxBankName.EditValue == null)
                    throw new Exception("开户行必须填写！");
                if (tbxBankNumber.EditValue == null)
                    throw new Exception("银行账号必须填写！");
                if (tbxTel.EditValue == null)
                    throw new Exception("座机电话必须填写！");
                if (cbxProvince.SelectedValue == null)
                    throw new Exception("所在省必须填写！");
                if (cbxCity.SelectedValue == null)
                    throw new Exception("所在市必须填写！");
                if (cbxDistrict.SelectedValue == null)
                    throw new Exception("所在县区必须填写！");
                if (cbxSBProvince.SelectedValue == null)
                    throw new Exception("所属省级税务机关必须选择！");
                if (cbxSBCity.SelectedValue == null)
                    throw new Exception("所属市级税务机关必须选择！");
                if (cbxSBDistrict.SelectedValue == null)
                    throw new Exception("所属县区局税务机关必须选择！");
                if (cbxTradeBig.SelectedValue == null)
                    throw new Exception("所属行业大类必须选择！");
                if (cbxTradeMid.SelectedValue == null)
                    throw new Exception("所属行业中类必须选择！");
                if (cbxDevice.SelectedValue == null)
                    throw new Exception("相关设备必须选择！");
                UserInfo um = new UserInfo();
                um.Id = 0;
                um.LoginName = tbxUserName.Text.Trim();
                um.Password = tbxPassword.Text.Trim();
                um.CompanyName = tbxCompanyName.Text.Trim();
                um.ContactName = tbxContact.Text.Trim();
                um.MobNum = tbxMobPhone.Text.Trim();
                um.TaxNum = tbxTaxNumber.Text.Trim();
                um.Address = tbxAddress.Text.Trim();
                um.BankName = tbxBankName.Text.Trim();
                um.BankNum = tbxBankNumber.Text.Trim();
                um.TelNum = tbxTel.Text.Trim();
                um.ProvinceId = Convert.ToInt32(cbxProvince.SelectedValue);
                um.ProvinceName = cbxProvince.Text;
                um.CityId = Convert.ToInt32(cbxCity.SelectedValue);
                um.CityName = cbxCity.Text;
                um.DistrictId = Convert.ToInt32(cbxDistrict.SelectedValue);
                um.DistrictName = cbxDistrict.Text;
                um.SBProvinceId = Convert.ToInt32(cbxSBProvince.SelectedValue);
                um.SBProvinceName = cbxSBProvince.Text;
                um.SBCityId = Convert.ToInt32(cbxSBCity.SelectedValue);
                um.SBCityName = cbxSBCity.Text;
                um.Id = Convert.ToInt32(cbxSBDistrict.SelectedValue);
                um.SBName = cbxSBDistrict.Text;
                um.TradeBigId = cbxTradeBig.SelectedValue.ToString();
                um.TradeBigName = cbxTradeBig.Text;
                um.TradeMidId = cbxTradeMid.SelectedValue.ToString();
                um.TradeMidName = cbxTradeMid.Text;
                um.TradeSmallId = cbxTradeSmall.SelectedValue.ToString();
                um.TradeSmallName = cbxTradeSmall.Text;
                um.State = Convert.ToInt32(cbxState.SelectedValue);
                um.DeviceId = Convert.ToInt32(cbxDevice.SelectedValue);
                um.DeviceRemark = cbxDevice.Text;
                um.ExpireTime = Convert.ToDateTime(dtpExpireDate.Value);
                Dictionary<string, string> dc = new Dictionary<string, string>();
                dc.Add("Token", Share.MoM.Token);

                RetModel rm = Share.PostData("51API/UserManage/UserAdd", dc, JsonConvert.SerializeObject(um));
                if (!rm.Success)
                {
                    MessageBox.Show("注册失败！\r\n错误代码:" + rm.ErrCode + "\r\n错误信息：" + rm.ErrInfo);
                    return;
                }
                MessageBox.Show("注册成功！");
                tbxUserName.Text = "";
                tbxPassword.Text = "";
                tbxCompanyName.Text = "";
                tbxContact.Text = "";
                tbxMobPhone.Text = "";
                tbxTaxNumber.Text = "";
                tbxAddress.Text = "";
                tbxTel.Text = "";
                tbxBankName.Text = "";
                tbxBankNumber.Text = "";
                cbxProvince.SelectedIndex = -1;
                cbxCity.SelectedIndex = -1;
                cbxDistrict.SelectedIndex = -1;
                cbxSBProvince.SelectedIndex = -1;
                cbxSBCity.SelectedIndex = -1;
                cbxDistrict.SelectedIndex = -1;
                cbxTradeBig.SelectedIndex = -1;
                cbxTradeMid.SelectedIndex = -1;
                cbxTradeSmall.SelectedIndex = -1;
                cbxState.SelectedIndex = -1;
                cbxDevice.SelectedIndex = -1;
                dtpExpireDate.Value = DateTime.Now;

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }


        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void barButtonItem2_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            try
            {
                Dictionary<string, string> dc = new Dictionary<string, string>();
                dc.Add("Token", Share.MoM.Token);
                UserInfo ui = new UserInfo();
                ui.Id = Sid;
                ui.LoginName = tbxUserName.Text.Trim();
                ui.Password = CryptUtil.Crypt.MD32Crypt(tbxPassword.Text.Trim());
                ui.CompanyName = tbxCompanyName.Text.Trim();
                ui.ContactName = tbxContact.Text.Trim();
                ui.MobNum = tbxMobPhone.Text.Trim();
                ui.TaxNum = tbxTaxNumber.Text.Trim();
                ui.Address = tbxAddress.Text.Trim();
                ui.TelNum = tbxTel.Text.Trim();
                ui.BankName = tbxBankName.Text.Trim();
                ui.BankNum = tbxBankNumber.Text.Trim();
                ui.ProvinceId = Convert.ToInt32(cbxProvince.SelectedValue);
                ui.ProvinceName = cbxProvince.Text;
                ui.CityId = Convert.ToInt32(cbxCity.SelectedValue);
                ui.CityName = cbxCity.Text;
                ui.DistrictId = Convert.ToInt32(cbxDistrict.SelectedValue);
                ui.DistrictName = cbxDistrict.Text;
                ui.SBProvinceId = Convert.ToInt32(cbxSBProvince.SelectedValue);
                ui.SBProvinceName = cbxSBProvince.Text;
                ui.SBCityId = Convert.ToInt32(cbxSBCity.SelectedValue);
                ui.SBCityName = cbxSBCity.Text;
                ui.SBId = Convert.ToInt32(cbxDistrict.SelectedValue);
                ui.SBName = cbxSBDistrict.Text;
                ui.TradeBigId = cbxTradeBig.SelectedValue.ToString();
                ui.TradeBigName = cbxTradeBig.Text;
                ui.TradeMidId = cbxTradeMid.SelectedValue.ToString();
                ui.TradeMidName = cbxTradeMid.Text;
                ui.TradeSmallId = cbxTradeSmall.SelectedValue.ToString();
                ui.TradeSmallName = cbxTradeSmall.Text;
                ui.State = Convert.ToInt32(cbxState.SelectedValue);
                ui.ExpireTime = dtpExpireDate.Value;
                ui.DeviceId = Convert.ToInt32(cbxDevice.SelectedValue);
                ui.DeviceRemark = cbxDevice.Text;
                RetModel rm = Share.PostData("51API/UserManage/UserEdit", dc, JsonConvert.SerializeObject(ui));
                if (rm.Success)
                {
                    MessageBox.Show("修改成功！");
                    tbxUserName.Text = "";
                    tbxPassword.Text = "";
                    tbxCompanyName.Text = "";
                    tbxContact.Text = "";
                    tbxMobPhone.Text = "";
                    tbxTaxNumber.Text = "";
                    tbxAddress.Text = "";
                    tbxTel.Text = "";
                    tbxBankName.Text = "";
                    tbxBankNumber.Text = "";
                    cbxProvince.SelectedIndex = -1;
                    cbxCity.SelectedIndex = -1;
                    cbxDistrict.SelectedIndex = -1;
                    cbxSBProvince.SelectedIndex = -1;
                    cbxSBCity.SelectedIndex = -1;
                    cbxDistrict.SelectedIndex = -1;
                    cbxTradeBig.SelectedIndex = -1;
                    cbxTradeMid.SelectedIndex = -1;
                    cbxTradeSmall.SelectedIndex = -1;
                    cbxState.SelectedIndex = -1;
                    cbxDevice.SelectedIndex = -1;
                    dtpExpireDate.Value = DateTime.Now;
                    barButtonItem4_ItemClick(sender, e);

                }
                else
                {
                    MessageBox.Show("修改失败！\r\n错误代码:" + rm.ErrCode + "\r\n错误信息：" + rm.ErrInfo);
                    return;
                }
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void barButtonItem3_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            try
            {
                Dictionary<string, string> dc = new Dictionary<string, string>();
                dc.Add("Token", Share.MoM.Token);
                UserInfo ui = new UserInfo();
                ui.Id = Sid;
                RetModel rm = Share.PostData("51API/UserManage/UserState", dc, JsonConvert.SerializeObject(ui));
                if (rm.Success)
                {
                    MessageBox.Show("删除成功！");
                    tbxUserName.Text = "";
                    tbxPassword.Text = "";
                    tbxCompanyName.Text = "";
                    tbxContact.Text = "";
                    tbxMobPhone.Text = "";
                    tbxTaxNumber.Text = "";
                    tbxAddress.Text = "";
                    tbxTel.Text = "";
                    tbxBankName.Text = "";
                    tbxBankNumber.Text = "";
                    cbxProvince.SelectedIndex = -1;
                    cbxCity.SelectedIndex = -1;
                    cbxDistrict.SelectedIndex = -1;
                    cbxSBProvince.SelectedIndex = -1;
                    cbxSBCity.SelectedIndex = -1;
                    cbxDistrict.SelectedIndex = -1;
                    cbxTradeBig.SelectedIndex = -1;
                    cbxTradeMid.SelectedIndex = -1;
                    cbxTradeSmall.SelectedIndex = -1;
                    cbxState.SelectedIndex = -1;
                    cbxDevice.SelectedIndex = -1;
                    dtpExpireDate.Value = DateTime.Now;
                    barButtonItem4_ItemClick(sender, e);
                }
                else
                {
                    MessageBox.Show("删除失败！\r\n错误代码:" + rm.ErrCode + "\r\n错误信息：" + rm.ErrInfo);
                    return;
                }
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void barButtonItem4_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            try
            {
                tbxUserName.Text = "";
                tbxPassword.Text = "";
                tbxCompanyName.Text = "";
                tbxContact.Text = "";
                tbxMobPhone.Text = "";
                tbxTaxNumber.Text = "";
                tbxAddress.Text = "";
                tbxTel.Text = "";
                tbxBankName.Text = "";
                tbxBankNumber.Text = "";
                cbxProvince.SelectedIndex = -1;
                cbxCity.SelectedIndex = -1;
                cbxDistrict.SelectedIndex = -1;
                cbxSBProvince.SelectedIndex = -1;
                cbxSBCity.SelectedIndex = -1;
                cbxDistrict.SelectedIndex = -1;
                cbxTradeBig.SelectedIndex = -1;
                cbxTradeMid.SelectedIndex = -1;
                cbxTradeSmall.SelectedIndex = -1;
                cbxState.SelectedIndex = -1;
                cbxDevice.SelectedIndex = -1;
                dtpExpireDate.Value = DateTime.Now;
                gridControl1.DataSource = null;
                Dictionary<string, string> dc = new Dictionary<string, string>();
                dc.Add("Token", Share.MoM.Token);
                RetModel ret = Share.PostData("51API/UserManage/GetUserList", dc, null);
                if (ret.Success)
                {
                    UserInfo[] um = JsonConvert.DeserializeObject<UserInfo[]>(ret.Data.ToString());
                    DataTable dt = new DataTable();
                    dt.Columns.Add("id");
                    dt.Columns.Add("登录名");
                    dt.Columns.Add("公司名");
                    dt.Columns.Add("联系人");
                    dt.Columns.Add("手机");
                    dt.Columns.Add("税号");
                    dt.Columns.Add("地址");
                    dt.Columns.Add("电话");
                    dt.Columns.Add("开户行");
                    dt.Columns.Add("银行账号");
                    dt.Columns.Add("ProvinceId");
                    dt.Columns.Add("ProvinceName");
                    dt.Columns.Add("CityId");
                    dt.Columns.Add("CityName");
                    dt.Columns.Add("DistrictId");
                    dt.Columns.Add("DistrictName");
                    dt.Columns.Add("SBProvinceId");
                    dt.Columns.Add("SBProvinceName");
                    dt.Columns.Add("SBCityId");
                    dt.Columns.Add("SBCityName");
                    dt.Columns.Add("SBDistrictId");
                    dt.Columns.Add("SBDistrictName");
                    dt.Columns.Add("TradeBigId");
                    dt.Columns.Add("TradeBigName");
                    dt.Columns.Add("TradeMidId");
                    dt.Columns.Add("TradeMidName");
                    dt.Columns.Add("TradeSmallId");
                    dt.Columns.Add("TradeSmallName");
                    dt.Columns.Add("DeviceID");
                    dt.Columns.Add("设备备注");
                    dt.Columns.Add("用户状态");
                    
                    dt.Columns.Add("到期日期");

                    for (int i = 0; i < um.Length; i++)
                    {
                        DataRow dr = dt.NewRow();
                        dr["id"] = um[i].Id;
                        dr["登录名"] = um[i].LoginName;
                        dr["公司名"] = um[i].CompanyName;
                        dr["联系人"] = um[i].ContactName;
                        dr["手机"] = um[i].MobNum;
                        dr["税号"] = um[i].TaxNum;
                        dr["地址"] = um[i].Address;
                        dr["电话"] = um[i].TelNum;
                        dr["开户行"] = um[i].BankName;
                        dr["银行账号"] = um[i].BankNum;
                        dr["ProvinceId"] = um[i].ProvinceId;
                        dr["ProvinceName"] = um[i].ProvinceName;
                        dr["CityId"] = um[i].CityId;
                        dr["CityName"] = um[i].CityName;
                        dr["DistrictId"] = um[i].DistrictId;
                        dr["DistrictName"] = um[i].DistrictName;
                        dr["SBProvinceId"] = um[i].SBProvinceId;
                        dr["SBProvinceName"] = um[i].SBProvinceName;
                        dr["SBCityId"] = um[i].SBCityId;
                        dr["SBCityName"] = um[i].SBCityName;
                        dr["SBDistrictId"] = um[i].SBId;
                        dr["SBDistrictName"] = um[i].SBName;
                        dr["TradeBigId"] = um[i].TradeBigId;
                        dr["TradeBigName"] = um[i].TradeBigName;
                        dr["TradeMidId"] = um[i].TradeMidId;
                        dr["TradeMidName"] = um[i].TradeMidName;
                        dr["TradeSmallId"] = um[i].TradeSmallId;
                        dr["TradeSmallName"] = um[i].TradeSmallName;
                        if (um[i].State == 0)
                            dr["用户状态"] = "正常";
                        if (um[i].State == 1)
                            dr["用户状态"] = "到续费期";
                        if (um[i].State == 2)
                            dr["用户状态"] = "手动禁用";
                        dr["DeviceID"] = um[i].DeviceId;
                        dr["设备备注"] = um[i].DeviceRemark;
                        dr["到期日期"] = um[i].ExpireTime;
                        dt.Rows.Add(dr);
                    }
                    gridControl1.DataSource = dt;
                    gridView1.Columns["id"].Visible = false;
                    gridView1.Columns["ProvinceId"].Visible = false;
                    gridView1.Columns["ProvinceName"].Visible = false;
                    gridView1.Columns["CityId"].Visible = false;
                    gridView1.Columns["CityName"].Visible = false;
                    gridView1.Columns["DistrictId"].Visible = false;
                    gridView1.Columns["DistrictName"].Visible = false;
                    gridView1.Columns["SBProvinceId"].Visible = false;
                    gridView1.Columns["SBProvinceName"].Visible = false;
                    gridView1.Columns["SBCityId"].Visible = false;
                    gridView1.Columns["SBCityName"].Visible = false;
                    gridView1.Columns["SBDistrictId"].Visible = false;
                    gridView1.Columns["SBDistrictName"].Visible = false;
                    gridView1.Columns["TradeBigId"].Visible = false;
                    gridView1.Columns["TradeBigName"].Visible = false;
                    gridView1.Columns["TradeMidId"].Visible = false;
                    gridView1.Columns["TradeMidName"].Visible = false;
                    gridView1.Columns["TradeSmallId"].Visible = false;
                    gridView1.Columns["TradeSmallName"].Visible = false;
                    gridView1.Columns["DeviceID"].Visible = false;
                }
                else
                {
                    MessageBox.Show("查询失败！\r\n错误代码:" + ret.ErrCode + "\r\n错误信息：" + ret.ErrInfo);
                    return;
                }
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 选中行
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void gridView1_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            tbxUserName.Text = "";
            tbxPassword.Text = "";
            tbxCompanyName.Text = "";
            tbxContact.Text = "";
            tbxMobPhone.Text = "";
            tbxTaxNumber.Text = "";
            tbxAddress.Text = "";
            tbxTel.Text = "";
            tbxBankName.Text = "";
            tbxBankNumber.Text = "";
            cbxProvince.SelectedIndex = -1;
            cbxCity.SelectedIndex = -1;
            cbxDistrict.SelectedIndex = -1;
            cbxSBProvince.SelectedIndex = -1;
            cbxSBCity.SelectedIndex = -1;
            cbxDistrict.SelectedIndex = -1;
            cbxTradeBig.SelectedIndex = -1;
            cbxTradeMid.SelectedIndex = -1;
            cbxTradeSmall.SelectedIndex = -1;
            cbxState.SelectedIndex = -1;
            cbxDevice.SelectedIndex = -1;
            dtpExpireDate.Value = DateTime.Now;


            Sid = Convert.ToInt32(gridView1.GetRowCellValue(gridView1.GetSelectedRows()[0], gridView1.Columns["id"]));
            SLoginName = gridView1.GetRowCellValue(gridView1.GetSelectedRows()[0], gridView1.Columns["登录名"]).ToString();
            SCompName = gridView1.GetRowCellValue(gridView1.GetSelectedRows()[0], gridView1.Columns["公司名"]).ToString();
            SContact = gridView1.GetRowCellValue(gridView1.GetSelectedRows()[0], gridView1.Columns["联系人"]).ToString();
            SMobPhone = gridView1.GetRowCellValue(gridView1.GetSelectedRows()[0], gridView1.Columns["手机"]).ToString();
            STaxNumber = gridView1.GetRowCellValue(gridView1.GetSelectedRows()[0], gridView1.Columns["税号"]).ToString();
            SAddress = gridView1.GetRowCellValue(gridView1.GetSelectedRows()[0], gridView1.Columns["地址"]).ToString();
            STelNum = gridView1.GetRowCellValue(gridView1.GetSelectedRows()[0], gridView1.Columns["电话"]).ToString();
            SBankName = gridView1.GetRowCellValue(gridView1.GetSelectedRows()[0], gridView1.Columns["开户行"]).ToString();
            SBankNum = gridView1.GetRowCellValue(gridView1.GetSelectedRows()[0], gridView1.Columns["银行账号"]).ToString();
            SProvinceId = Convert.ToInt32(gridView1.GetRowCellValue(gridView1.GetSelectedRows()[0], gridView1.Columns["ProvinceId"]));
            SProvinceName = gridView1.GetRowCellValue(gridView1.GetSelectedRows()[0], gridView1.Columns["ProvinceName"]).ToString();
            SCityId = Convert.ToInt32(gridView1.GetRowCellValue(gridView1.GetSelectedRows()[0], gridView1.Columns["CityId"]));
            SCityName = gridView1.GetRowCellValue(gridView1.GetSelectedRows()[0], gridView1.Columns["CityName"]).ToString();
            SDistrictId = Convert.ToInt32(gridView1.GetRowCellValue(gridView1.GetSelectedRows()[0], gridView1.Columns["DistrictId"]));
            SDistrictName = gridView1.GetRowCellValue(gridView1.GetSelectedRows()[0], gridView1.Columns["DistrictName"]).ToString();
            SSBProvinceId = gridView1.GetRowCellValue(gridView1.GetSelectedRows()[0], gridView1.Columns["SBProvinceId"]).ToString();
            SSBProvinceName = gridView1.GetRowCellValue(gridView1.GetSelectedRows()[0], gridView1.Columns["SBProvinceName"]).ToString();
            SSBCityId = gridView1.GetRowCellValue(gridView1.GetSelectedRows()[0], gridView1.Columns["SBCityId"]).ToString();
            SSBCityName = gridView1.GetRowCellValue(gridView1.GetSelectedRows()[0], gridView1.Columns["SBCityName"]).ToString();
            STradeBigId = gridView1.GetRowCellValue(gridView1.GetSelectedRows()[0], gridView1.Columns["TradeBigId"]).ToString();
            STradeBigName = gridView1.GetRowCellValue(gridView1.GetSelectedRows()[0], gridView1.Columns["TradeBigName"]).ToString();
            STradeMidId = gridView1.GetRowCellValue(gridView1.GetSelectedRows()[0], gridView1.Columns["STradeMidId"]).ToString();
            STradeMidName = gridView1.GetRowCellValue(gridView1.GetSelectedRows()[0], gridView1.Columns["STradeMidName"]).ToString();
            STradeSmallId = gridView1.GetRowCellValue(gridView1.GetSelectedRows()[0], gridView1.Columns["TradeSmallId"]).ToString();
            STradeSmallName = gridView1.GetRowCellValue(gridView1.GetSelectedRows()[0], gridView1.Columns["TradeSmallName"]).ToString();
            string s1 = gridView1.GetRowCellValue(gridView1.GetSelectedRows()[0], gridView1.Columns["State"]).ToString();
            SDeviceID = Convert.ToInt32(gridView1.GetRowCellValue(gridView1.GetSelectedRows()[0], gridView1.Columns["DeviceID"]));
            SDeviceRemark = gridView1.GetRowCellValue(gridView1.GetSelectedRows()[0], gridView1.Columns["设备备注"]).ToString();
            if (s1 == "正常")
                SState = 0;
            if (s1 == "到续费期")
                SState = 1;
            if (s1 == "手动禁用")
                SState = 2;
            SExpireDate = Convert.ToDateTime(gridView1.GetRowCellValue(gridView1.GetSelectedRows()[0], gridView1.Columns["到期日期"]));

            //对界面控件赋值
            tbxUserName.Text = SLoginName;
            tbxPassword.Text = "";
            tbxCompanyName.Text = SCompName;
            tbxContact.Text = SContact;
            tbxMobPhone.Text = SMobPhone;
            tbxTaxNumber.Text = STaxNumber;
            tbxAddress.Text = SAddress;
            tbxTel.Text = STelNum;
            tbxBankName.Text = SBankName;
            tbxBankNumber.Text = SBankNum;
            cbxProvince.SelectedIndex = SProvinceId;
            cbxCity.SelectedIndex = SCityId;
            cbxDistrict.SelectedIndex = SDistrictId;
            cbxSBProvince.SelectedIndex = Convert.ToInt32(SSBProvinceId);
            cbxSBCity.SelectedIndex = Convert.ToInt32(SSBCityId);
            cbxDistrict.SelectedIndex = Convert.ToInt32(SSBDistrictId);
            cbxTradeBig.SelectedIndex = Convert.ToInt32(STradeBigId);
            cbxTradeMid.SelectedIndex = Convert.ToInt32(STradeMidId);
            cbxTradeSmall.SelectedIndex = Convert.ToInt32(STradeSmallId);
            cbxState.SelectedIndex = SState;
            cbxDevice.SelectedIndex = Convert.ToInt32(SDeviceID);
            
            dtpExpireDate.Value = SExpireDate;
            

        }

        
    }
}
